#include <windows.h>
#include <tchar.h>
#include "TextView.h"
#include "TextViewImpl.h"

LRESULT WINAPI CTextView::OnPaint()
{
	HDC			hdc;
	PAINTSTRUCT ps;
	RECT		rect;
	TCHAR		szText[] = _T("Welcome to TextPro!");

	HANDLE		hOldFont;
	HFONT		hFont;

	hdc = BeginPaint(m_hWnd, &ps);

	hFont = (HFONT) GetStockObject(ANSI_FIXED_FONT);
	hOldFont = SelectObject(hdc, hFont);

	GetClientRect(m_hWnd, &rect);
	ExtTextOut(hdc, 10, 10, ETO_OPAQUE, &rect, szText, _tcslen(szText), 0);
	SelectObject(hdc, hOldFont);
	EndPaint(m_hWnd, &ps);

	return 0;
}

LRESULT WINAPI TextViewWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	CTextView *ptv = (CTextView *) GetWindowLong(hwnd, 0);

	switch(msg)
	{
	case WM_NCCREATE:

		if((ptv = new CTextView(hwnd)) == 0)
			return FALSE;

		SetWindowLong(hwnd, 0, (LONG)ptv);
		return TRUE;

	case WM_NCDESTROY:

		delete ptv;
		return 0;

	case WM_PAINT:
		return ptv->OnPaint();

	default:
		break;
	}

	return DefWindowProc(hwnd, msg, wParam, lParam);
}

BOOL InitTextView()
{
	WNDCLASSEX	wcx;

	wcx.cbSize			= sizeof(wcx);
	wcx.style			= 0;
	wcx.lpfnWndProc		= TextViewWndProc;
	wcx.cbClsExtra		= 0;
	wcx.cbWndExtra		= sizeof(CTextView *);
	wcx.hInstance		= GetModuleHandle(0);
	wcx.hIcon			= 0;
	wcx.hCursor			= LoadCursor (NULL, IDC_IBEAM);
	wcx.hbrBackground	= (HBRUSH) 0;
	wcx.lpszMenuName	= 0;
	wcx.lpszClassName	= TEXTVIEW_CLASS;	
	wcx.hIconSm			= 0;

	return RegisterClassEx(&wcx) ? TRUE : FALSE;
}

HWND CreateTextView(HWND hwndParent)
{
	return CreateWindowEx(WS_EX_CLIENTEDGE, 
		TEXTVIEW_CLASS, _T(""), 
		WS_VSCROLL |WS_HSCROLL | WS_CHILD | WS_VISIBLE,
		0, 0, 0, 0, 
		hwndParent, 
		0, 
		GetModuleHandle(0), 
		0);
}
